import datetime

import pytz
from redis import Redis

from social.rdm import redis_instance


class BaseModel:
    db: Redis = redis_instance()


class ChatCountRdm(BaseModel):

    @classmethod
    def get_chat_count(cls, from_id, to_id):
        now = datetime.datetime.now()
        date_tz = now.astimezone(pytz.timezone('Asia/Shanghai'))
        d = date_tz.strftime('%Y-%m-%d')
        name = f'{d}:{from_id}:{to_id}'
        count = cls.db.get(name)
        return int(count) if count else 0

    @classmethod
    def set_chat_count(cls, from_id, to_id, count):
        now = datetime.datetime.now()
        date_tz = now.astimezone(pytz.timezone('Asia/Shanghai'))
        d = date_tz.strftime('%Y-%m-%d')
        name = f'{d}:{from_id}:{to_id}'
        cls.db.setex(name, 60 * 60 * 24, count)
